package com.app.knock.db;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import z.lib.base.CommonAndroid;
import z.lib.base.LogUtils;
import z.lib.base.BaseUtils;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class AvataList extends SkypeTable {
	public AvataList(Context context) {
		super(context);
		addColumns(id);
		addColumns(url);
//		addColumns(avata);
//		addColumns(slug);
//		addColumns(url);
//		addColumns(thumbnail);
//		addColumns(download_url);
//		addColumns(external_url);
//		addColumns(is_platinum_accessed);
//		addColumns(platinum_accessed_msg);
//		addColumns(type);
//		addColumns(user_id);
//		addColumns(subtitle);
//		addColumns(page);
	}

//	public static final String subtitle = "subtitle";
//	public static final String page = "page";
	public static final String id = "id";
	public static final String url = "url";
	public static final String avata = "avata";
//	public static final String content = "content";
//	public static final String slug = "slug";
//	public static final String url = "url";
//	public static final String thumbnail = "thumbnail";
//	public static final String download_url = "download_url";
//	public static final String external_url = "external_url";
//	public static final String is_platinum_accessed = "is_platinum_accessed";
//	public static final String platinum_accessed_msg = "platinum_accessed_msg";
//	public static final String type = "type";
//	public static final String user_id = "user_id";
	private static final String TAG = "AvataList";

	@Override
	public int getIndex() {
		return 2;
	}

	public void updateAvataList(String response) {
//		String user_idStr = new Account(getContext()).getUserId();
		try {
			JSONObject json = new JSONObject(response);
			String is_succes = CommonAndroid.getString(json, BaseUtils.KEY.is_success);

			if (BaseUtils.VALUE.STATUS_API_SUCCESS.equals(is_succes)) {
				JSONArray array = json.getJSONObject("data").getJSONArray("avatars");

				for (int i = 0; i < array.length(); i++) {
					JSONObject object = array.getJSONObject(i);
					String idStr = CommonAndroid.getString(object, id);

					if (!CommonAndroid.isBlank(idStr)) {
						String where = String.format("%s = '%s' ", id, idStr );
						ContentValues values = new ContentValues();

						values.put(id, CommonAndroid.getString(object, id));
						values.put(url, CommonAndroid.getString(object, url));
//						values.put(subtitle, CommonAndroid.getString(object, subtitle));
//						values.put(page, CommonAndroid.getString(object, page));
//						values.put(content, CommonAndroid.getString(object, content));
//						values.put(slug, CommonAndroid.getString(object, slug));
//						values.put(url, CommonAndroid.getString(object, url));
//						values.put(thumbnail, CommonAndroid.getString(object, thumbnail));
//						values.put(download_url, CommonAndroid.getString(object, download_url));
//						values.put(external_url, CommonAndroid.getString(object, external_url));
//						values.put(is_platinum_accessed, CommonAndroid.getString(object, is_platinum_accessed));
//						values.put(platinum_accessed_msg, CommonAndroid.getString(object, platinum_accessed_msg));
//						values.put(type, CommonAndroid.getString(object, type));
//						values.put(user_id, user_idStr);

						if (has(where)) {
							getContext().getContentResolver().update(getContentUri(), values, where, null);
						} else {
							getContext().getContentResolver().insert(getContentUri(), values);
						}
					}
				}
			}
		} catch (Exception e) {
		}
	}
	
	public Cursor querryAvata(String id2) {
		String where = String.format("%s = '%s' ", id, id2 );
		return querry(where);
	}

	public String getAvata(String id) {
		String suser_id = "";
		try {
			Cursor cursor = querryAvata(id);
			if (cursor != null) {
				if (cursor.moveToNext()) {
					suser_id = CommonAndroid.getString(cursor, url);
				}
				cursor.close();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return suser_id;
	}

}
